package com.acargil.sudoku.view.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.acargil.sudoku.model.ISudokuEngine;
import com.acargil.sudoku.model.PuzzleSolvingAlgorithm;
import com.acargil.sudoku.view.ISudokuView;
import com.acargil.sudoku.view.util.MessageFactory;

public class SolveListener implements ActionListener {

	private ISudokuEngine model;
	private ISudokuView view;

	public SolveListener(ISudokuEngine model, ISudokuView view) {
		this.model = model;
		this.view = view;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		int solvedGrid[][];
		String title, message, cmd = e.getActionCommand();
		switch (PuzzleSolvingAlgorithm.valueOf(cmd)) {
		case BASIC_ALGORITHM:
			solvedGrid = model.solve(PuzzleSolvingAlgorithm.BASIC_ALGORITHM);
			view.setAllCells(solvedGrid);
			
			message = PuzzleSolvingAlgorithm.BASIC_ALGORITHM.toString();
			title = PuzzleSolvingAlgorithm.BASIC_ALGORITHM.getTitle();
			MessageFactory.informationMessage(message, title);
			break;
		case COLLOQUIAL_ALGORITHM:
			solvedGrid = model.solve(PuzzleSolvingAlgorithm.COLLOQUIAL_ALGORITHM);
			view.setAllCells(solvedGrid);
			
			message = PuzzleSolvingAlgorithm.COLLOQUIAL_ALGORITHM.toString();
			title = PuzzleSolvingAlgorithm.COLLOQUIAL_ALGORITHM.getTitle();
			MessageFactory.informationMessage(message, title);
			break;
		default:
			message = "Couldn' t solve; problem occured, sorry!";
			MessageFactory.warningMessage(message);
			break;
		}
	}
}
